using System;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using FAQComponents;
using FAQComponents.Objects;
using FAQComponents.API;
using Adge.UI.WebControls;

namespace Adge.Web.Faq.Admin
{
	/// <summary>
	/// Summary description for CategoryAdmin.
	/// </summary>
	public partial class CategoryAdmin : CategoryRepeaterPage
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			PermissionAPI.EnsureAdministrator();

			if (!Page.IsPostBack)
				BindData();
		}

		CategoryCollection categories = null; 
		private void BindData()
		{
			categories = CategoryAPI.GetAllCategories();
			categories.Sort();

			rptCategories.DataSource = categories;
			rptCategories.DataBind();

			CategoryCollection csAdd = CategoryAPI.GetAllCategories();
			Category c = new Category();
			c.CategoryID = Globals.NULLIntegerValue;
			c.GroupID = -1;
			c.Name = "-- TOP-LEVEL CATEGORY --";
			c.Depth = 0;
			csAdd.Insert(0, c);

			rptCategoriesAdd.DataSource = csAdd;
			rptCategoriesAdd.DataBind();
		}
		
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.rptCategories.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.rptCategories_ItemDataBound);
			this.rptCategories.ItemCommand += new System.Web.UI.WebControls.RepeaterCommandEventHandler(this.rptCategories_ItemCommand);

		}
		#endregion

		#region Add Category-Related Event Handlers
		protected void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.pnlEditCategoriesUI.Visible = true;
			this.pnlCreateCategoryUI.Visible = false;

			ClearInputFormFields();
			BindData();
		}
		
		protected void lnkAddCategory_Click(object sender, System.EventArgs e)
		{
			this.pnlEditCategoriesUI.Visible = false;
			this.pnlCreateCategoryUI.Visible = true;
		}

		protected void btnAddCategory_Click(object sender, System.EventArgs e)
		{
			Category c = new Category();
			c.Name = nameAdd.Text;
			c.Description = descriptionAdd.Text;
			c.ParentCategoryID = InvariantString.ToInt32(Request.Form["categoryAdd"]);

			CategoryAPI.AddCategory(c);

			this.pnlEditCategoriesUI.Visible = true;
			this.pnlCreateCategoryUI.Visible = false;

			ClearInputFormFields();
			BindData();
		}

		private void ClearInputFormFields()
		{
			nameAdd.Text = descriptionAdd.Text = string.Empty;
		}
		#endregion

		#region Repeater Event Handlers
		private void rptCategories_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
		{
			int categoryID = -1;

			switch (e.CommandName)
			{
				case "Delete":
					// Delete the category
					categoryID = InvariantString.ToInt32(e.CommandArgument.ToString());
					CategoryAPI.DeleteCategory(categoryID);
					BindData();
					break;

				case "MoveUp":
					categoryID = InvariantString.ToInt32(e.CommandArgument.ToString());
					CategoryAPI.MoveCategorySortOrderUp(categoryID);
					BindData();
					break;

				case "MoveDown":
					categoryID = InvariantString.ToInt32(e.CommandArgument.ToString());
					CategoryAPI.MoveCategorySortOrderDown(categoryID);
					BindData();
					break;
			}
		}

		private void rptCategories_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				string catID = DataBinder.Eval(e.Item.DataItem, "CategoryID").ToString();

				LinkButton db = (LinkButton) e.Item.FindControl("btnDeleteCategory");
				db.CommandArgument = catID;				
				db.Enabled = !((bool) DataBinder.Eval(e.Item.DataItem, "HasChildCategory"));
				if (db.Enabled)
					db.Attributes["onclick"] = "javascript:if (!confirm('This will permanently delete this category and all FAQs within the category.  Are you sre you want to do this?')) return false; else return confirm('Are you really sure?  I mean, 100%, absolutely, positively sure?');";

				ImageButton moveUp = (ImageButton) e.Item.FindControl("btnMoveUp");
				ImageButton moveDown = (ImageButton) e.Item.FindControl("btnMoveDown");

				moveUp.CommandArgument = moveDown.CommandArgument = catID;


				if (e.Item.ItemIndex == 0)
					moveUp.Visible = false;
				else
					moveUp.Visible = categories[e.Item.ItemIndex].Depth <= categories[e.Item.ItemIndex - 1].Depth;
				
				if (e.Item.ItemIndex == categories.Count - 1)
					moveDown.Visible = false;
				else
					moveDown.Visible = categories[e.Item.ItemIndex].Depth == categories[e.Item.ItemIndex + 1].Depth;
			}
	}
		#endregion

	}
}
